package org.example;
//哈哈
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class GamePanel extends JPanel implements ActionListener {
    private SnakeGame main;
    private Timer timer;
    private SkillManager p1skillManager;
    private SkillManager p2skillManager;

    // 网格常量
    private static final int TILE_SIZE = 25;
    private static final int GRID_WIDTH = 30;
    private static final int GRID_HEIGHT = 30;
    private static final int GAME_WIDTH = TILE_SIZE * GRID_WIDTH;
    private static final int GAME_HEIGHT = TILE_SIZE * GRID_HEIGHT;
    // 速度
    private int speed = 150;//毫秒


    // 长度减少控制
    private int shrinkCounter = 0;
    private static final int SHRINK_THRESHOLD = 60; // 触发减少的循环次数

    // 游戏状态
    private boolean gameRunning = false;
    private boolean gameOver = false;
    private boolean paused = false;

    // 游戏元素
    private Snake snake;
    private Snake snake2 = null; // 第二条蛇

    private Foods foods;
    private Buildings buildings;
    // 游戏时间
    private double time = 0;
    private int timeLimit = 0;
    //皮肤
    private int skinType1 = 0;
    private int skinType2 = 0;
    // 游戏设置
    private int score = 0;
    private int score2 = 0; // 玩家2分数
    private String mapType = "无障碍";
    private String p1skillType = "无";
    private String p2skillType = "无";
    private String modeType = "经典模式";
    private String playerMode ="单人模式";
    private String  modeType2 = "生存模式";
    private String timeMode = "无";
    private int result = -1;

    public GamePanel(SnakeGame main) {
        this.main = main;
        setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);

        // 添加焦点监听器
        addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                setFocusable(true);
                requestFocusInWindow();
            }
        });

        setupKeyBindings();
    }

    private void setupKeyBindings() {
        InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getActionMap();

        // 定义按键映射
        KeyStroke[] keyStrokes = {
                KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_W, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_S, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_A, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_D, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), // p1
                KeyStroke.getKeyStroke(KeyEvent.VK_M, 0), // p2
                KeyStroke.getKeyStroke(KeyEvent.VK_P, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)
        };
        // 添加按键动作
        for (KeyStroke keyStroke : keyStrokes) {
            inputMap.put(keyStroke, keyStroke.toString());
            actionMap.put(keyStroke.toString(), new KeyAction(keyStroke));
        }
    }

    private class KeyAction extends AbstractAction {
        private final KeyStroke keyStroke;

        public KeyAction(KeyStroke keyStroke) {
            this.keyStroke = keyStroke;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            handleKey(keyStroke.getKeyCode());
        }
    }

    private void handleKey(int keyCode) {
        if (gameRunning && !gameOver) {
            switch(keyCode) {
                case KeyEvent.VK_UP:
                    if (snake2!=null)
                        snake2.setDirection(SnakeGame.Direction.UP);
                    break;
                case KeyEvent.VK_W:
                    snake.setDirection(SnakeGame.Direction.UP);
                    break;
                case KeyEvent.VK_DOWN:
                    if (snake2!=null)
                        snake2.setDirection(SnakeGame.Direction.DOWN);
                    break;
                case KeyEvent.VK_S:
                    snake.setDirection(SnakeGame.Direction.DOWN);
                    break;
                case KeyEvent.VK_LEFT:
                    if (snake2!=null)
                        snake2.setDirection(SnakeGame.Direction.LEFT);
                    break;
                case KeyEvent.VK_A:
                    snake.setDirection(SnakeGame.Direction.LEFT);
                    break;
                case KeyEvent.VK_RIGHT:
                    if (snake2!=null)
                        snake2.setDirection(SnakeGame.Direction.RIGHT);
                    break;
                case KeyEvent.VK_D:
                    snake.setDirection(SnakeGame.Direction.RIGHT);
                    break;
                case KeyEvent.VK_Q: // 按Q键激活技能
                    p1skillManager.activateSkill();
                    break;
                case KeyEvent.VK_M: // 按M键激活技能
                    if (snake2!=null)
                        p2skillManager.activateSkill();
                    break;
                case KeyEvent.VK_P:
                    paused = !paused;
                    repaint();
                    break;
                case KeyEvent.VK_ESCAPE:
                    main.showMenu();
                    break;

            }
        } else if (gameOver) {
            if (keyCode == KeyEvent.VK_SPACE) {
                startNewGame(mapType, p1skillType,p2skillType , modeType, playerMode,timeMode,skinType1,skinType2);
            } else if (keyCode == KeyEvent.VK_ESCAPE) {
                main.showMenu();
            }
        }
    }
    public void startNewGame(String mapType, String p1skillType, String p2skillType,String modeType, String playerMode,String timeMode,int skinType1,int skinType2) {
        this.mapType = mapType;
        this.p1skillType = p1skillType;
        this.p2skillType = p2skillType;
        this.modeType = modeType;
        this.playerMode = playerMode;
        p1skillManager = new SkillManager(p1skillType);
        p2skillManager = new SkillManager(p2skillType);
        this.timeMode = timeMode;
        this.skinType1 = skinType1;
        this.skinType2 = skinType2;
        // 初始化蛇 - 使用网格坐标
        snake = new Snake(3, 1, TILE_SIZE, skinType1);
        if (playerMode.equals("双人模式")) {
            // 第二条蛇初始化（放在地图另一侧）
            snake2 = new Snake( 3, GRID_HEIGHT - 2, TILE_SIZE, skinType2);
        }
        // 初始化障碍物 - 使用网格坐标
        buildings = new Buildings(GRID_WIDTH, GRID_HEIGHT, TILE_SIZE);
        if (mapType.equals("有固定障碍")) {
            buildings.generateMap1();
        }
        else if (mapType.equals("迷宫")) {
            buildings.generateMap2();
        }
        else if (mapType.equals("乱石")) {
            buildings.generateMap3();
        }
        else if (mapType.equals("宝藏")){
            buildings.generateMap4();
        }

        // 初始化食物 - 使用网格坐标
        foods = new Foods(GRID_WIDTH, GRID_HEIGHT, TILE_SIZE);
        if (mapType.equals("宝藏")){
            for (int i = 14; i < 17; i++) {
                for (int j = 14; j < 17; j++) {
                    foods.addFood(i, j, new Color(255, 215, 0), buildings);
                }
            }
        }
        if (playerMode.equals("双人模式")){
            for (int i = 0; i < 3; i++) {
                foods.generateFood(snake, snake2, buildings, mapType);
            }
        }
        else {
            for (int i = 0; i < 3; i++) {
                foods.generateFood(snake,buildings, mapType);
            }
        }
        // 重置计数器
        shrinkCounter = 0;

        // 游戏状态
        gameRunning = true;
        gameOver = false;
        paused = false;
        score = 0;
        score2 = 0;
        time = 0;
        timeLimit = 0;

        // 启动游戏循环
        if (timer != null) timer.stop();
        timer = new Timer(speed, this);
        timer.start();

        // 确保获得焦点
        setFocusable(true);
        requestFocusInWindow();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (gameRunning && !paused) {
            // 增加计数器
            if (modeType.equals("饥饿模式")) {
                shrinkCounter++;
            }

            if (playerMode.equals("单人模式")) {
                if(p1skillManager.getSkillType().equals("穿梭") && p1skillManager.isTransportActive()){
                    snake.move(4);
                    p1skillManager.setTransportActive(false);
                }
                else if (p1skillManager.getSkillType().equals("冲刺") && p1skillManager.isSprintActive()){
                    for (int i = 0; i < 4; i++){
                        snake.move(1);
                        foodCheck(snake);
                        crashCheck(snake);
                    }
                    snake.move(1);
                    p1skillManager.setSprintActive(false);
                }
                else {
                    snake.move(1);
                }
            }
            else if (playerMode.equals("双人模式")) {
                if(p1skillManager.getSkillType().equals("穿梭") && p1skillManager.isTransportActive()){
                    snake.move(4);
                    p1skillManager.setTransportActive(false);
                }
                else if (p1skillManager.getSkillType().equals("冲刺") && p1skillManager.isSprintActive()){
                    for (int i = 0; i < 4; i++){
                        snake.move(1);
                        foodCheck(snake,snake2);
                        crashCheck(snake,snake2);
                    }
                    snake.move(1);
                    p1skillManager.setSprintActive(false);
                }
                else {
                    snake.move(1);
                }
                if(p2skillManager.getSkillType().equals("穿梭") && p2skillManager.isTransportActive()){
                    snake2.move(4);
                    p2skillManager.setTransportActive(false);
                }
                else if (p2skillManager.getSkillType().equals("冲刺") && p2skillManager.isSprintActive()){
                    for (int i = 0; i < 4; i++){
                        snake2.move(1);
                        foodCheck(snake,snake2);
                        crashCheck(snake,snake2);
                    }
                    snake2.move(1);
                    p2skillManager.setSprintActive(false);
                }
                else {
                    snake2.move(1);
                }
            }

            // 检查是否吃到食物
            if (playerMode.equals("单人模式"))
                foodCheck(snake);
            if (playerMode.equals("双人模式"))
                foodCheck(snake,snake2);
            //碰撞检测
            if (playerMode.equals("单人模式"))
                crashCheck(snake);
            if (playerMode.equals("双人模式")){
                crashCheck(snake,snake2);
            }
            if (playerMode.equals("双人模式")){
                if (snake.isAlive() && snake2.isAlive()) {
                    Point head1 = snake.getHead();
                    Point head2 = snake2.getHead();
                    // 检查是否头碰头
                    if (head1.getGridX() == head2.getGridX() && head1.getGridY() == head2.getGridY()) {
                        int len1 = snake.getBody().size();
                        int len2 = snake2.getBody().size();

                        if (len1 == len2) {
                            // 平局
                            result = 0;
                            gameOver();

                        } else if (len1 < len2) {
                            // 玩家1输（蛇短）
                            result = 2;
                            snake.die();
                            gameOver();
                        } else {
                            // 玩家2输（蛇短）
                            result = 1;
                            snake2.die();
                            gameOver();
                        }
                    }
                    if(isCollidingWithOtherSnakeBody(snake, snake2)){
                        result = 2;
                        snake2.die();
                        gameOver();
                    }
                    if(isCollidingWithOtherSnakeBody(snake2, snake)){
                        result = 1;
                        snake.die();
                        gameOver();
                    }
                }
            }
            //时间限制
            time = time + timer.getDelay() / 1000.0;
            char firstChar = timeMode.charAt(0);
            timeLimit = (firstChar - '0') * 60;
            if (playerMode.equals("单人模式") && !timeMode.equals("无")){
                if (time >= timeLimit){
                    gameOver();
                }
            }
            if(playerMode.equals("双人模式") && !timeMode.equals("无")){
                if (time >= timeLimit){
                    if(score > score2)
                        result = 1;
                    else if(score < score2)
                        result = 2;
                    else
                        result = 0;
                    gameOver();
                }
            }
            // 模式效果：过段时间减少长度
            if (modeType.equals("饥饿模式") && shrinkCounter >= SHRINK_THRESHOLD) {
                snake.shrink();
                if (playerMode.equals("双人模式"))
                    snake2.shrink();
                shrinkCounter = 0; // 重置计数器
            }
            if (playerMode.equals("单人模式")) {
                if(snake.getBody().size() <= 0 || snake.isAlive() == false){
                    gameOver();
                }
            }
            else if (playerMode.equals("双人模式")){
                if(snake2.getBody().size() <= 0 && snake.getBody().size() <= 0){
                    result = 0;
                    gameOver();
                }
                else if(snake.getBody().size() <= 0 ){
                    result = 2;
                    snake.die();
                    gameOver();
                }
                else if(snake2.getBody().size() <= 0){
                    result = 1;
                    snake2.die();
                    gameOver();
                }
            }
        }
        repaint();
    }
    // 检查是否与另一条蛇的身体碰撞（不包括头部）
    private boolean isCollidingWithOtherSnakeBody(Snake snake, Snake otherSnake) {
        Point head = snake.getHead();
        // 从第1个元素开始（跳过头部）
        for (int i = 1; i < otherSnake.getBody().size(); i++) {
            Point segment = otherSnake.getBody().get(i);
            if (head.getGridX() == segment.getGridX() && head.getGridY() == segment.getGridY()) {
                return true;
            }
        }
        return false;
    }

    // 检查是否吃到食物
    private void foodCheck(Snake snake) {
        if (!snake.isAlive())
            return;
        Point food = foods.checkCollision(snake.getHead());
        if (food != null) {
            foods.generateFood(snake, buildings, mapType);
            int foodScore = 10;

            // 技能效果
            if (p1skillManager.isDoubleScoreActive()) {
                foodScore *= 2;
                if (food.getColor().equals(new Color(255, 215, 0))) { // 金色技能食物
                    p1skillManager.addCharge();
                    p1skillManager.addCharge();
                    score += foodScore;
                    snake.grow();
                    snake.grow();
                }
                else if (food.getColor().equals(new Color(128, 0, 128))){
                    snake.shrink();
                    snake.shrink();
                    score -= foodScore;
                }
                else {
                    snake.grow();
                    snake.grow();
                    score += foodScore;
                }
                p1skillManager.consumeDoubleScore();
            }
            else {
                if (food.getColor().equals(new Color(255, 215, 0))) { // 金色技能食物
                    p1skillManager.addCharge();
                    score += foodScore;
                    snake.grow();
                }
                else if (food.getColor().equals(new Color(128, 0, 128))){
                    snake.shrink();
                    score -= foodScore;
                }
                else {
                    score += foodScore;
                    snake.grow();
                }
            }

            // 加速模式效果
            if (modeType.equals("加速模式") && timer.getDelay() > 50) {
                timer.setDelay(timer.getDelay() - 10);
            }
        }
    }
    private void foodCheck(Snake snake, Snake snake2) {
        if (snake.isAlive()) {
            Point food = foods.checkCollision(snake.getHead());
            if (food != null) {
                foods.generateFood(snake, buildings, mapType);
                int foodScore = 10;

                // 技能效果
                if (p1skillManager.isDoubleScoreActive()) {
                    foodScore *= 2;
                    if (food.getColor().equals(new Color(255, 215, 0))) { // 金色技能食物
                        p1skillManager.addCharge();
                        p1skillManager.addCharge();
                        score += foodScore;
                        snake.grow();
                        snake.grow();
                    }
                    else if (food.getColor().equals(new Color(128, 0, 128))){
                        score -= foodScore;
                        snake.shrink();
                        snake.shrink();
                    }
                    else {
                        score += foodScore;
                        snake.grow();
                        snake.grow();
                    }
                    p1skillManager.consumeDoubleScore();
                }
                else {
                    if (food.getColor().equals(new Color(255, 215, 0))) { // 金色技能食物
                        p1skillManager.addCharge();
                        score += foodScore;
                        snake.grow();
                    }
                    else if (food.getColor().equals(new Color(128, 0, 128))){
                        score -= foodScore;
                        snake.shrink();
                    }
                    else {
                        score += foodScore;
                        snake.grow();
                    }
                }

                // 加速模式效果
                if (modeType.equals("加速模式") && timer.getDelay() > 50) {
                    timer.setDelay(timer.getDelay() - 10);
                }
            }
        }
        if (snake2.isAlive()) {
            Point food = foods.checkCollision(snake2.getHead());
            if (food != null) {
                foods.generateFood(snake2, buildings, mapType);
                int foodScore = 10;

                // 技能效果
                if (p2skillManager.isDoubleScoreActive()) {
                    foodScore *= 2;
                    if (food.getColor().equals(new Color(255, 215, 0))) { // 金色技能食物
                        p2skillManager.addCharge();
                        p2skillManager.addCharge();
                        score2 += foodScore;
                        snake2.grow();
                        snake2.grow();
                    }
                    else if (food.getColor().equals(new Color(128, 0, 128))){
                        score2 -= foodScore;
                        snake2.shrink();
                        snake2.shrink();
                    }
                    else {
                        score2 += foodScore;
                        snake2.grow();
                        snake2.grow();
                    }
                    p2skillManager.consumeDoubleScore();
                }
                else {
                    if (food.getColor().equals(new Color(255, 215, 0))) { // 金色技能食物
                        p2skillManager.addCharge();
                        score2 += foodScore;
                        snake2.grow();
                    }
                    else if (food.getColor().equals(new Color(128, 0, 128))){
                        score2 -= foodScore;
                        snake2.shrink();
                    }
                    else {
                        score2 += foodScore;
                        snake2.grow();
                    }
                }
                // 加速模式效果
                if (modeType.equals("加速模式") && timer.getDelay() > 50) {
                    timer.setDelay(timer.getDelay() - 10);
                }
            }
        }
    }
    //碰撞检测
    private void crashCheck(Snake snake) {
        // 无障碍模式边界穿越处理
        if (mapType.equals("无障碍")) {
            handleBoundaryCrossing(snake);
        }
        // 检查是否撞墙（有边界模式）
        else {
            Point head = snake.getHead();
            if (head.getGridX() < 0 || head.getGridX() >= GRID_WIDTH ||
                    head.getGridY() < 0 || head.getGridY() >= GRID_HEIGHT) {
                snake.die();
                gameOver();
            }
        }
        // 检查是否撞到障碍物
        if (buildings.checkCollision(snake.getHead())) {
            snake.die();
            gameOver();
        }
        // 检查是否撞到自己
        if (snake.checkSelfCollision()) {
            if (snake.getBody().size()>2) {
                snake.die();
                gameOver();
            }
        }
    }
    private void crashCheck(Snake snake,Snake snake2) {
        if (!snake.isAlive() || !snake2.isAlive()){
            if (snake.isAlive()){
                result = 1;
                crashCheck(snake);
            }
            else if (snake2.isAlive()){
                result = 2;
                crashCheck(snake2);
            }
        }
        else {
            // 无障碍模式边界穿越处理
            if (mapType.equals("无障碍")) {
                handleBoundaryCrossing(snake);
                handleBoundaryCrossing(snake2);
            }
            // 检查是否撞墙（有边界模式）
            else {
                Point head = snake.getHead();
                Point head2 = snake2.getHead();
                if ((head.getGridX() < 0 || head.getGridX() >= GRID_WIDTH || head.getGridY() < 0 || head.getGridY() >= GRID_HEIGHT) &&
                        (head2.getGridX() < 0 || head2.getGridX() >= GRID_WIDTH || head2.getGridY() < 0 || head2.getGridY() >= GRID_HEIGHT))
                {
                    result = 0;
                    gameOver();
                }
                else if ((head.getGridX() < 0 || head.getGridX() >= GRID_WIDTH || head.getGridY() < 0 || head.getGridY() >= GRID_HEIGHT)) {
                    result = 2;
                    snake.die();
                    gameOver();
                }
                else if ((head2.getGridX() < 0 || head2.getGridX() >= GRID_WIDTH || head2.getGridY() < 0 || head2.getGridY() >= GRID_HEIGHT)) {
                    result = 1;
                    snake2.die();
                    gameOver();
                }
            }
            // 检查是否撞到障碍物
            if (buildings.checkCollision(snake.getHead()) && buildings.checkCollision(snake2.getHead())) {
                result = 0;
                gameOver();
            }
            else if (buildings.checkCollision(snake.getHead())) {
                result = 2;
                snake.die();
                gameOver();
            }
            else if (buildings.checkCollision(snake2.getHead())) {
                result = 1;
                snake2.die();
                gameOver();
            }
            // 检查是否撞到自己
            if (snake.checkSelfCollision() && snake2.checkSelfCollision()) {
                if (snake.getBody().size()>2 && snake2.getBody().size()>2) {
                    result = 0;
                    gameOver();
                }
            }
            else if (snake.checkSelfCollision()) {
                if (snake.getBody().size()>2) {
                    result = 2;
                    snake.die();
                    gameOver();
                }
            }
            else if (snake2.checkSelfCollision()) {
                if (snake2.getBody().size()>2) {
                    result = 1;
                    snake2.die();
                    gameOver();
                }
            }
        }
    }

//哈哈
    // 边界穿越处理方法
    private void handleBoundaryCrossing(Snake snake) {
        Point head = snake.getHead();
        int newGridX = head.getGridX();
        int newGridY = head.getGridY();

        // 处理水平边界穿越
        if (newGridX < 0) {
            newGridX = GRID_WIDTH - 1;
        } else if (newGridX >= GRID_WIDTH) {
            newGridX = 0;
        }

        // 处理垂直边界穿越
        if (newGridY < 0) {
            newGridY = GRID_HEIGHT - 1;
        } else if (newGridY >= GRID_HEIGHT) {
            newGridY = 0;
        }

        // 如果位置发生变化，更新蛇头位置
        if (newGridX != head.getGridX() || newGridY != head.getGridY()) {
            head.setGridX(newGridX);
            head.setGridY(newGridY);
        }
    }

    private void gameOver() {
        if (playerMode.equals("单人模式")){
            result = -1;
            gameRunning = false;
            gameOver = true;
            timer.stop();
        }
        else if (playerMode.equals("双人模式")){
            if (!snake.isAlive()){
                for (Point segment : snake.getBody())
                    foods.addFood(segment.getGridX(), segment.getGridY(), new Color(220, 60, 60), buildings);
            }
            if (!snake2.isAlive()){
                for (Point segment : snake2.getBody())
                    foods.addFood(segment.getGridX(), segment.getGridY(), new Color(220, 60, 60), buildings);
            }
            if (!snake.isAlive() && !snake2.isAlive()) {
                gameRunning = false;
                gameOver = true;
                timer.stop();
            }
        }

        if(result == 0 || time >= timeLimit){
            gameRunning = false;
            gameOver = true;
            timer.stop();
        }
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 绘制游戏区域
        g2d.setColor(new Color(20, 20, 30));
        g2d.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

        // 绘制网格
        g2d.setColor(new Color(40, 40, 40));
        for (int i = 0; i < GRID_WIDTH; i++) {
            g2d.drawLine(i * TILE_SIZE, 0, i * TILE_SIZE, GAME_HEIGHT);
        }
        for (int i = 0; i < GRID_HEIGHT; i++) {
            g2d.drawLine(0, i * TILE_SIZE, GAME_WIDTH, i * TILE_SIZE);
        }

        // 绘制障碍物
        buildings.draw(g2d);

        // 绘制食物
        foods.draw(g2d);

        // 绘制蛇
        if (snake != null) {
            snake.draw(g2d);
            if (playerMode.equals("双人模式")) {
                snake2.draw(g2d);
            }
        }

        // 绘制分数
        g2d.setColor(Color.WHITE);
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 20));
        g2d.drawString("p1分数: " + score, 20, 30);
        g2d.drawString("p1长度: " + snake.getBody().size(), 150, 30);
        if (playerMode.equals("双人模式")) {
            g2d.drawString("p2分数: " + score2, 20, 50);
            g2d.drawString("p2长度: " + snake2.getBody().size(), 150, 50);
        }
        g2d.drawString("地图: " + mapType, 300, 30);
        g2d.drawString("技能: " + p1skillType, 450, 30);
        g2d.drawString("模式: " + modeType, 600, 30);

        // 绘制技能信息
        g2d.setColor(Color.YELLOW);
        g2d.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g2d.drawString(p1skillManager.getSkillInfo(), 20, GAME_HEIGHT - 40);
        if (playerMode.equals("双人模式"))
            g2d.drawString(p2skillManager.getSkillInfo(), 20, GAME_HEIGHT - 20);

        // 绘制冷却信息
        //g2d.setColor(Color.CYAN);
        //g2d.drawString(skillManager.getCooldownInfo(), 20, GAME_HEIGHT - 15);
        // 在"过段时间减少长度"模式下显示倒计时
        if (modeType.equals("饥饿模式") && gameRunning) {
            int countdown = SHRINK_THRESHOLD - shrinkCounter;
            int seconds = (int)(countdown * (timer.getDelay() / 1000.0));

            g2d.setColor(new Color(200, 50, 50, 200));
            g2d.fillRect(GAME_WIDTH - 150, 10, 140, 25);

            g2d.setColor(Color.WHITE);
            g2d.drawString("减少倒计时: " + seconds + "秒", GAME_WIDTH - 145, GAME_HEIGHT-10);
        }

        if (!timeMode.equals("无")) {
            g2d.setColor(new Color(200, 50, 50, 200));
            g2d.fillRect(GAME_WIDTH - 150, 10, 140, 25);

            g2d.setColor(Color.WHITE);
            g2d.drawString("时间: " + String.format("%.1f", time) + "秒", GAME_WIDTH - 290, GAME_HEIGHT-10);
        }

        // 游戏结束信息
        if (gameOver) {
            g2d.setColor(new Color(255, 50, 50, 200));
            g2d.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

            g2d.setColor(Color.WHITE);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 48));
            g2d.drawString("游戏结束", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 - 50);

            g2d.setFont(new Font("微软雅黑", Font.BOLD, 36));
            if (playerMode.equals("单人模式")) {
                g2d.drawString("最终分数: " + score, GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 20);
                g2d.setFont(new Font("微软雅黑", Font.PLAIN, 24));
                g2d.drawString("按空格键重新开始", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 80);
                g2d.drawString("按ESC键返回菜单", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 120);
            }
            if (playerMode.equals("双人模式")){
                g2d.drawString("p1最终分数: " + score, GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 20);
                g2d.drawString("p2最终分数: " + score2, GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 80);

                if(result == 0)
                    g2d.drawString("平局", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 120);
                if(result == 1)
                    g2d.drawString("p1获胜", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 120);
                if(result == 2)
                    g2d.drawString("p2获胜", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 120);
                g2d.setFont(new Font("微软雅黑", Font.PLAIN, 24));
                g2d.drawString("按空格键重新开始", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 160);
                g2d.drawString("按ESC键返回菜单", GAME_WIDTH/2 - 120, GAME_HEIGHT/2 + 200);
            }
        }

        // 暂停信息
        if (paused) {
            g2d.setColor(new Color(0, 0, 0, 150));
            g2d.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);

            g2d.setColor(Color.WHITE);
            g2d.setFont(new Font("微软雅黑", Font.BOLD, 48));
            g2d.drawString("游戏暂停", GAME_WIDTH/2 - 120, GAME_HEIGHT/2);

            g2d.setFont(new Font("微软雅黑", Font.PLAIN, 24));
            g2d.drawString("按P键继续游戏", GAME_WIDTH/2 - 100, GAME_HEIGHT/2 + 60);
        }
    }
}